package exp2;

public class Complex {
    // 定义属性
    private double RealPart;
    private double ImagePart;

    // 定义构造函数
    public Complex(){}
    public Complex(double R,double I)
    {
        this.RealPart = R;
        this.ImagePart = I;
    }

    // getter,setter
    public void setRealPart(double r)
    {
        this.RealPart = r;
    }

    public double getRealPart()
    {
        return this.RealPart;
    }

    public void setImagePart(double i)
    {
        this.ImagePart = i;
    }

    public double getImagePart()
    {
        return this.ImagePart;
    }

    // 定义公有方法:加减乘除
    public Complex ComplexAdd(Complex a)
    {
        return new Complex(RealPart + a.getRealPart(),ImagePart + a.ImagePart);
    }

    public Complex ComplexSub(Complex a)
    {
        return new Complex(RealPart - a.getRealPart(),ImagePart - a.ImagePart);
    }

    public Complex ComplexMulti(Complex a)
    {
        return new Complex(RealPart * a.getRealPart() - ImagePart * a.getImagePart(),
                RealPart * a.getImagePart() + ImagePart * a.getRealPart());
    }

    public Complex ComplexDiv(Complex a)
    {
        return new Complex((RealPart * a.getRealPart() + ImagePart * a.getImagePart()) / (a.getRealPart() * a.getRealPart() + a.getImagePart() * a.getImagePart()),
                ((ImagePart * a.getRealPart())-(RealPart *   a.getImagePart())) / (a.getRealPart() * a.getRealPart() + a.getImagePart() * a.getImagePart()));
    }

    // Override Object
    public boolean equals(Complex obj)
    {
        if (RealPart == obj.getRealPart() && ImagePart == obj.getImagePart() )
            return true;
        else
            return false;
    }

    @Override
    public String toString()
    {
        String result;
        if (ImagePart > 0)
            result = "(" + RealPart + "+" + ImagePart +"i" + ")";
        else
            if (ImagePart < 0)
                result = "(" + RealPart + "" + ImagePart +"i" + ")";
            else
                result = RealPart + "" ;

        return result;
    }
}
